<template>
  <div class="app-container">

    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="商店名称" style="width: 250px;" class="filter-item" @keyup.enter.native="searchList" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="searchList">
        搜索
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleAdd">
        新增商家
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%; margin-bottom:20px;"
    >
      <el-table-column label="商家名称" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="商家logo">
        <template slot-scope="scope">
          <el-image
            style="width: 60px;"
            :src="scope.row.logo"
            :preview-src-list="[scope.row.logo]"
          >
            <div slot="placeholder" class="image-slot" style="width:60px;height:30px;text-align:center;line-height:30px;">
              <i class="dot el-icon-loading" style="font-size:20px;" />
            </div>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="联系人" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.contact || '暂无' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.contactPhone || '暂无' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="edit(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pagesize" @pagination="getList" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 700px; margin-left:50px;">
        <el-form-item label="商家名称" prop="name">
          <el-input v-model="temp.name" type="text" autofocus />
        </el-form-item>
        <el-form-item label="姓名" prop="contact">
          <el-input v-model="temp.contact" type="text" />
        </el-form-item>
        <el-form-item label="手机号" prop="contactPhone">
          <el-input v-model="temp.contactPhone" type="text" />
        </el-form-item>
        <el-form-item label="商家logo">
          <Single-image v-model="temp.logo" :single-image="true" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateOrderDetail()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { delSupplier, getSupplier, saveSupplier } from '@/api/supplier'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

import SingleImage from '@/components/Upload/SingleImage'
import { renderTime } from '@/utils/util'
import { phoneReg } from '@/utils/validate.js'

export default {
  name: 'Orders',
  components: { Pagination, SingleImage },
  directives: { waves },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        name: '',
        page: 1, // 当前页
        pagesize: 10// 每页数量
      },
      temp: {
        name: '',
        logo: '',
        contact: '',
        contactPhone: '',
        gid: ''
      },
      rules: {
        name: [{ required: true, message: '请输入商家姓名', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
        contactPhone: [{ required: true, message: '请输入手机号', trigger: 'blur' }, { pattern: phoneReg, message: '请输入正确手机号', trigger: 'blur' }]
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '更新',
        create: '新增'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    renderTime,
    // 1：域名 2：邮箱 3：网站4：安全
    getList() {
      this.listLoading = true
      getSupplier({ page: this.listQuery.page, pagesize: this.listQuery.pagesize, keyword: this.listQuery.name }).then(res => {
        this.total = res.result.total
        this.list = res.result.list
        this.listLoading = false
      })
    },
    searchList() {
      this.listQuery.page = 1
      this.getList()
    },
    edit(row) {
      this.temp.name = row.name || ''
      this.temp.logo = row.logo || ''
      this.temp.contact = row.contact || ''
      this.temp.contactPhone = row.contactPhone || ''
      this.temp.gid = row.gid || ''
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    // 更新表格内容
    updateOrderDetail() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveSupplier(this.temp).then(res => {
            this.$notify({
              title: '成功',
              message: '商家更新成功~',
              type: 'success',
              duration: 2000
            })
          })
          this.getList()
          this.dialogFormVisible = false
        }
      })
    },
    // 添加表格内容
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          saveSupplier(this.temp).then(res => {
            this.$notify({
              title: '成功',
              message: '商家添加成功~',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm(`是否确认要删除${row.name}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delSupplier({ gid: row.gid }).then(res => {
          this.$notify({
            message: '删除成功！',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => {})
    },
    // 添加了新的供货商
    handleAdd() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.temp.name = ''
      this.temp.contact = ''
      this.temp.contactPhone = ''
      this.temp.logo = ''
      this.temp.gid = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    }

  }
}
</script>
<style lang="scss">
    @import '@/assets/scss/mixin.scss';
    .el-form-item--medium{
      .el-form-item__content{
        display: flex;
        align-items: center;
        .el-input{
          &+.el-button{
            margin-left: 0.625rem;
          }
        }
        .el-button{
          &:not(:first-child){
            margin-left: 0.625rem;
          }
        }
      }
    }
</style>
